/**
 * @file       st7735.h
 * @author     mz8023yt
 * @email      mz8023yt@163.com
 * @version    V1.0
 * @date       2022-03-13
 * @brief      st7735 lcd driver
 */

#ifndef _ST7735_H_
#define _ST7735_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

#ifndef LOW
#define LOW 0
#endif

#ifndef HIGH
#define HIGH 1
#endif

#define RGB565_WHITE   0xFFFF
#define RGB565_BLACK   0x0000
#define RGB565_BLUE    0x001F
#define RGB565_BRED    0XF81F
#define RGB565_GRED    0XFFE0
#define RGB565_GBLUE   0X07FF
#define RGB565_RED     0xF800
#define RGB565_MAGENTA 0xF81F
#define RGB565_GREEN   0x07E0
#define RGB565_CYAN    0x7FFF
#define RGB565_YELLOW  0xFFE0
#define RGB565_BROWN   0XBC40
#define RGB565_BRRED   0XFC07
#define RGB565_GRAY    0X8430

#define ST7735_FLIP_NONE 0
#define ST7735_FLIP_X    1
#define ST7735_FLIP_Y    2
#define ST7735_FLIP_XY   3

enum rotate_t {
	ROTATE_0,
	ROTATE_90,
	ROTATE_180,
	ROTATE_270,
};

struct cmds {
	uint8_t cmd;
	uint8_t dats[16];
	uint8_t nums; // 0xff = end of cmds.
};


/**
 * @brief ST7735
 */
struct st7735 {
	struct {
		struct {
			uint8_t width;  // 132 max
			uint8_t height; // 162 max
			uint8_t xshift;
			uint8_t yshift;
		} fix;
		struct {
			int orien;
		} var;
	} cfg;
	struct {
		struct {
			void (*write)(uint8_t* bytes, uint32_t len);
		} spi;
		struct {
			void (*msleep)(int ms);
		} time;
		struct {
			void (*rst)(int val);
			void (*bl)(int val);
			void (*dc)(int val);
		} pin;
	} ops;
};

void st7735_init(struct st7735* lcd);
void st7735_point_draw(struct st7735* lcd, uint8_t x, uint8_t y, uint16_t color);
void st7735_rect_fill(struct st7735* lcd, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t* color);
void st7735_rect_fill_color(struct st7735* lcd, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void st7735_rotate_set(struct st7735 *lcd, enum rotate_t rotate);

#ifdef __cplusplus
}
#endif

#endif /* _ST7735_H_ */
